package com.atguigu.gulimall.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gulimall.member.dao.MemberLevelDao;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.exception.PhoneException;
import com.atguigu.gulimall.member.exception.UsernameException;
import com.atguigu.gulimall.member.utils.HttpUtils;
import com.atguigu.gulimall.member.vo.MemberUserLoginVo;
import com.atguigu.gulimall.member.vo.MemberUserRegisterVo;
import com.atguigu.gulimall.member.vo.SocialUser;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(MemberUserRegisterVo vo) {

        MemberEntity memberEntity = new MemberEntity();

        //设置默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(levelEntity.getId());

        //设置其他的默认信息
        //检查用户名和手机号是否唯一。感知异常，异常机制
        checkPhoneUnique(vo.getPhone());
        checkUserNameUnique(vo.getUserName());

        memberEntity.setNickname(vo.getUserName());
        memberEntity.setUsername(vo.getUserName());

        //MD5
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(vo.getPassword());

        memberEntity.setPassword(encode);
        memberEntity.setMobile(vo.getPhone());
        memberEntity.setGender(0);
        memberEntity.setCreateTime(new Date());

        //保存数据
        this.baseMapper.insert(memberEntity);


    }
    /**
     * 登入
     *
     * */
    @Override
    public MemberEntity login(MemberUserLoginVo vo) {

        String loginacct = vo.getLoginacct();
        String password = vo.getPassword();
        //1.去数据库查询   loginacct 可以是用户名也可以是手机号  用户名因为之前检查过唯一性
        MemberDao baseMapper = this.baseMapper;
        MemberEntity entitie = baseMapper.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username", loginacct)  //用户名
                .or()
                .eq("mobile", loginacct));//手机号
        if (entitie!=null){
            //spring  MD5 解密
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            //密码与数据库的进行匹配
            boolean matches = passwordEncoder.matches(entitie.getPassword(), password);
            if(matches){
                return entitie;
            }else {
                return null;
            }
        }else {
            return null;
        }

    }
/*
*     社区登入
* */
    @Override
    public MemberEntity loginOauth2(SocialUser socialUser) throws Exception {
        //具有登录和注册逻辑
        String uid = socialUser.getUid();
        //1、判断当前社交用户是否已经登录过系统
        MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", uid));
        if (memberEntity!=null){
            //这个用户已经注册过
            //更新用户的访问令牌的时间和access_token
            MemberEntity update = new MemberEntity();
            update.setId(memberEntity.getId());
            update.setAccessToken(socialUser.getAccess_token());
            update.setExpiresIn(socialUser.getExpires_in());
            this.baseMapper.updateById(update);

            memberEntity.setAccessToken(socialUser.getAccess_token());
            memberEntity.setExpiresIn(socialUser.getExpires_in());
            return memberEntity;
        }else {
            //2、没有查到当前社交用户对应的记录我们就需要注册一个
            MemberEntity register = new MemberEntity();
            //3、查询当前社交用户的社交账号信息（昵称、性别等）https://api.weibo.com/2/users/show.json
            HashMap<String, String> map = new HashMap<>();
            map.put("access_token",socialUser.getAccess_token());
            map.put("uid",socialUser.getUid());
            HttpResponse response = HttpUtils.doGet("https://api.weibo.com", "/2/users/show.json", "GET", new HashMap<>(), map);
          if(response.getStatusLine().getStatusCode()==200){
              //查询成功

              String json = EntityUtils.toString(response.getEntity());
              JSONObject jsonObject = JSON.parseObject(json);
              String name = jsonObject.getString("name");
              String gender = jsonObject.getString("gender");
              String profileImageUrl = jsonObject.getString("profile_image_url");

              register.setNickname(name);
              register.setGender("m".equals(gender)?1:0);
              register.setHeader(profileImageUrl);
              register.setCreateTime(new Date());
              register.setSocialUid(socialUser.getUid());
              register.setAccessToken(socialUser.getAccess_token());
              register.setExpiresIn(socialUser.getExpires_in());

              //把用户信息插入到数据库中
              this.baseMapper.insert(register);

          }
            return register;
        }
    }

    private void checkPhoneUnique(String phone) {
        Integer phoneCount = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));

        if (phoneCount > 0) {
            throw new PhoneException();
        }
    }

    private void checkUserNameUnique(String serName) {

        Integer username = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", serName));

        if (username>0){
            throw new UsernameException();
        }
    }

}